package com.juitar.base.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import com.juitar.base.Chord;
import com.juitar.base.Tuning;

/**
 * @author SKopylov
 */
public class Config {

	static List<Chord> chordList = new ArrayList<Chord>();

	static List<Tuning> tuningList = new ArrayList<Tuning>();

	static boolean initialized = false;

	public static List<Chord> getChords() {
		if (!initialized) {
			readConfig();
		}
		return chordList;

	}

	public static List<Tuning> getTunings() {
		if (!initialized) {
			readConfig();
		}
		return tuningList;
	}

	static private void readConfig() {
		
		InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.txt");
		
		//InputStream in = ClassLoader.getSystemResourceAsStream("config.txt");
		InputStreamReader inReader = new InputStreamReader(in);
		BufferedReader r = new BufferedReader(inReader);
		String line = null;
		int state = 0;
		try {
			while ((line = r.readLine()) != null) {
				line = line.trim();
				if (line.compareTo("") == 0)
					continue;
				if (line.startsWith("#"))
					continue;
				if (line.startsWith("[Chords]")) {
					state = 1;
				} else if (line.startsWith("[Tunings]")) {
					state = 2;
				} else if (state == 1) {
					chordList.add(Chord.create(line));
				} else if (state == 2) {
					tuningList.add(Tuning.create(line));
				} else {
					; // OK
				}
			}
		} catch (IOException e) {
			System.out.println(e);
			e.printStackTrace();
		}
		initialized = true;
	}
}
